/**
 * name: 新手引导管理器
 * author: wyd
 * date: 2018/10/13
 */
class GuideManager {
    private static _instance:GuideManager;
    public UI:GuideUI;
    //是否在引导中
    public isInGuide:boolean = false;
    //引导进行的大步骤
    public curGuideId:number = 0;
    //引导进行的小步骤
    public curGuideSubId:number = 0;

    //新手引导list
    //引导大步骤和引导小步骤
    //X配成-1表示在屏幕右侧
    //Y配成小数代表百分比的屏幕高度
    //isCanTouchTrough 是否点击穿透 预留字段方便后面扩展
	//type 0矩形 1圆形
	//node 需要让出的node控件名称
	//logic 需要抓取的界面名称
    private list = {
         1:{            
                guide_1:{
                    isCanTouchTrough : false, 
					isShowMask:false
                },
                guide_2:{
                    Tips:"左右滑动屏幕移动鱼钩",
                    isCanTouchTrough : false,
					isShowMask:true,
					radius:100,
					type:0
                },
                guide_3:{
                    Tips:"升级捕鱼技能获得更多收益",
					width:674,
					height:331,
					type:1,
                    isCanTouchTrough : false,
					isShowMask:true
                }
        }
    }

	public constructor() {
	}

	public static instance():GuideManager{
		if(!GuideManager._instance){
			GuideManager._instance = new GuideManager();
		}
		return GuideManager._instance;
	}

    public showGuide(data)
    {
        if(data)
        {
            this.curGuideId = data["curGuideId"];
            this.curGuideSubId = data["curGuideSubId"];
            
            let Data = this.getDataByCurIdAndSubId();
            if(Data)
            {
                if(!this.UI){
                    this.UI = new GuideUI(Data);
                }
                if(this.UI && !this.UI.parent){
                    GlobalManager.instance().addChildToLayer(this.UI, LayerType.Guide);
                }
				if(Data.isCanTouchTrough)
				this.UI.touchEnabled = true;
            }
        }
    }

    public hideGuide()
    {
        this.curGuideId = 0;
        this.curGuideSubId = 0;
        this.isInGuide = false;
        if(this.UI && this.UI.parent){
            this.UI.parent.removeChild(this.UI);
            this.UI = null;
        }
    }

    //通过大步骤和小步骤拿取数据
    public getDataByCurIdAndSubId()
    {
        let str = "guide_"+this.curGuideSubId;
        if(this.list[this.curGuideId][str])
            return this.list[this.curGuideId][str];
        else
        {
            LogUtil.error("不存在的引导Id");
            return null;
        }
    }

	//通过大步骤和小步骤判断是否在某个引导中
    public judgeIsInGuide(Id, id)
    {
        if(this.isInGuide)
		{
			if(this.curGuideId == Id && this.curGuideSubId == id)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		return false;
    }

	//通过大步骤和小步骤判断是否完成了某个引导
    public judgeIsCompleteGuide(Id, id)
    {
        return (GuideCache.instance().getGuideStatusByIdAndSubId(Id, id));
    }

    /**
	 * 通知引导步骤变更
     *  - guidance_id:1
     *  - step : guidance_1 //第一步
	 */
	public postGuideStepChange(call?:Function, scope?){
		let str = '';

		let arr = GuideCache.instance().getGuideInfo()[1];
        let openid = RoleCache.instance().openid ? RoleCache.instance().openid : '';

		for(let k in arr){
			str += arr[k] + '_';
		}
		str = str.slice(0, str.length-1); 

		let data={
			openid:openid,
			guidance:str
		}

		HttpManager.instance().sendPost(GameConst.httpUrl + 'fishingaccs/update_guidance', StringUtil.jsonToHttpPostString(data), (e)=>{
            let Data:Array<Object> = JSON.parse(e);
            if(Data && Data["data"])
            {
                GuideCache.instance().saveGuideInfo(Data["data"]);
                this.hideGuide();
                if(call)
                {
                    call.call(scope);
                }
            }
		}, this);
	}
}